import { customToast } from '@/utils/tips'
const Toast = customToast;

export function formValidation(params){
    const { data, rules } = params; 
    return new Promise((resolve,reject)=>{
        if(Object.prototype.toString.call(data) === '[object Object]' && Object.prototype.toString.call(rules) === '[object Object]'){
        	for(let k in rules){
        		const y =  rules[k];
        		if(Array.isArray(y)){
        			for( let s in y ){
        				if((y[s].required && !data[k]) || (y[s].validate && !y[s].validate.test(data[k]))){
        					console.warn(y[s].message);
        					Toast.fail(y[s].message);
        					return;
        				}
        			}
        		}
        	}
        	setTimeout(()=>{
        		return resolve();
        	})
        }else{
            return reject({
    			data:{ message:'数据格式有误或校验规则不正确' }
    		})
        }
    })
}